package com.zhuangxiaoyan.designpattern.clonemodel;

/**
 * @Classname CloneModel
 * @Description
   1）优点是比较好理解，简单易操作
   2）在创建新的对象时，总是需要重新获取原始对象的属性，如果创建的对象比较复杂时，效率较低
   3）总是需要重新初始化对象，而不是动态地获得对象运行时的状态，不够灵活
 * @Date 2022/3/27 9:16
 * @Created by xjl
 */
public class CloneModel {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Sheep sheep = new Sheep("Tom", 1,"白色");
            System.out.println(sheep);
        }
    }

    public static void test(String[] args) {
        Sheep sheep = new Sheep("jerry", 10, "红色");
        for (int i = 0; i < 10; i++) {
            Sheep sheep1 = (Sheep) sheep.clone();
            System.out.println(sheep1);
        }
    }
}

/**
 * @description 原型类
  * @param: null
 * @date: 2022/3/27 9:17
 * @return:
 * @author: xjl
*/
class Sheep extends Object {
    private String name;
    private Integer age;
    private String color;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Sheep(String name, Integer age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    /**
     * @description 重写的clone的方法
      * @param:
     * @date: 2022/3/27 9:29
     * @return: java.lang.Object
     * @author: xjl
    */
    @Override
    protected Object clone() {
        Sheep sheep = null;
        try {
            sheep = (Sheep) super.clone();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sheep;
    }

}

